package com.hexiang.jdbc;

import com.hexiang.dao.OmsOrderItemDao;
import com.hexiang.domain.OmsOrderItem;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 基于JDBC的daoimpl
 */
@Repository
public class OmsOrderItemJdbcDaoImpl implements OmsOrderItemDao {

    @Resource
    private DataSource dataSource;

    protected Connection getConn() throws SQLException {
        return dataSource.getConnection();
    }

    protected void closeConn(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }

    @Override
    public int[][] batchSave(List<OmsOrderItem> omsOrderItemList) {
        return new int[0][];
    }

    @Override
    public int[][] batchUpdate(List<OmsOrderItem> tist) {
        return new int[0][];
    }

    @Override
    public Long save(OmsOrderItem omsOrderItem) {
        Long pkId = null;
        Connection conn = null;
        try {
            conn = this.getConn();
            PreparedStatement ps = conn.prepareStatement("INSERT INTO t_oms_order_item (order_id, order_sn, product_id, product_pic, product_name, product_brand, product_sn, product_price, product_quantity, product_sku_id, product_sku_code, product_category_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
            ps.setLong(1, omsOrderItem.getOrderId());
            ps.setString(2, omsOrderItem.getOrderSn());
            ps.setLong(3, omsOrderItem.getProductId());
            ps.setString(4, omsOrderItem.getProductPic());
            ps.setString(5, omsOrderItem.getProductName());
            ps.setString(6, omsOrderItem.getProductBrand());
            ps.setString(7, omsOrderItem.getProductSn());
            ps.setBigDecimal(8, omsOrderItem.getProductPrice());
            ps.setInt(9, omsOrderItem.getProductQuantity());
            ps.setLong(10, omsOrderItem.getProductSkuId());
            ps.setString(11, omsOrderItem.getProductSkuCode());
            ps.setLong(12, omsOrderItem.getProductCategoryId());

            ps.executeUpdate();
            // 检索由于执行此 Statement 对象而创建的所有自动生成的键
            ResultSet rs = ps.getGeneratedKeys();
            if (rs.next()) {
                pkId = rs.getLong(1);
            }
            ps.close();
            conn.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                this.closeConn(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return pkId;
    }

    @Override
    public OmsOrderItem get(long id) {
        return null;
    }

    @Override
    public Integer deleteById(long id) {
        return null;
    }

    @Override
    public Integer update(OmsOrderItem omsOrderItem) {
        return null;
    }

    @Override
    public List<OmsOrderItem> queryAll() {
        List<OmsOrderItem> list = new ArrayList<>();

        Connection conn = null;
        try {
            conn = this.getConn();
            StringBuilder sql = new StringBuilder("select * from t_oms_order_item ; ");

            PreparedStatement ps = conn.prepareStatement(sql.toString());
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                OmsOrderItem omsOrderItem = new OmsOrderItem();
                omsOrderItem.setId(rs.getLong("id"));
                omsOrderItem.setOrderId(rs.getLong("order_id"));
                omsOrderItem.setOrderSn(rs.getString("order_sn"));
                omsOrderItem.setProductId(rs.getLong("product_id"));
                omsOrderItem.setProductPic(rs.getString("product_pic"));
                omsOrderItem.setProductName(rs.getString("product_name"));
                omsOrderItem.setProductBrand(rs.getString("product_brand"));
                omsOrderItem.setProductSn(rs.getString("product_sn"));
                omsOrderItem.setProductPrice(rs.getBigDecimal("product_price"));
                omsOrderItem.setProductQuantity(rs.getInt("product_quantity"));
                omsOrderItem.setProductSkuId(rs.getLong("product_sku_id"));
                omsOrderItem.setProductSkuCode(rs.getString("product_sku_code"));
                omsOrderItem.setProductCategoryId(rs.getLong("product_category_id"));
                list.add(omsOrderItem);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                this.closeConn(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public List<OmsOrderItem> queryListByLimit(int limit) {
        return null;
    }

    @Override
    public Integer count() {
        return null;
    }
}
